<!DOCTYPE html>
<html>
	<head>
		<meta charset="utf-8">
		<title></title>
		<link rel="stylesheet" type="text/css" href="../../src/css/reset.css" />
		<link rel="stylesheet" type="text/css" href="../../src/css/main.css" />
		<link rel="stylesheet" type="text/css" href="../../libs/leaflet/leaflet.css" />
		<script src="../../libs/leaflet/leaflet.js" type="text/javascript" charset="utf-8"></script>
		<script src="../../libs/leaflet/leaflet.ChineseTmsProviders.js" type="text/javascript" charset="utf-8"></script>
	</head>
	<body>
		<div id="map"></div>
		<script type="text/javascript"  id="my_script">
			let map = L.map('map', {
				center: [32.444721222054795, 119.4224873962402], // leaflet 所有坐标都是纬度在前,经度在后的
				zoom: 16
			})

			// 地图依赖于leaflet.ChineseTmsProviders.js  他提供4326坐标系的天地图,高德地图等
			let baseLayer = L.tileLayer.chinaProvider('TianDiTu.Normal.Map', {
				maxZoom: 18,
				minZoom: 10
			})
			map.addLayer(baseLayer)
			let labelLayer = L.tileLayer.chinaProvider('TianDiTu.Normal.Annotion', {
				maxZoom: 18,
				minZoom: 10
			})
			map.addLayer(labelLayer)

			// 测距依赖于distanceTo方法
			// 下面是较为复杂的应用
			// 点击事件
			let lines = null
			map.on("click", mapClick)
			map.on("contextmenu",()=>{
				lines.remove()
				map.off("mousemove")
				map.off("click")
				map.on("click",mapClick)
			})

			function mapClick(e) {
				let points = [
					[e.latlng.lat, e.latlng.lng]
				]
				let tempLines = L.polyline(points, {
					color: '#E8542B'
				}).addTo(map)
				lines = L.polyline(points, {
					color: '#E8542B'
				}).addTo(map)
				let start = addPoint(e.latlng)
				start.bindTooltip("起点", {
					permanent: true, 
					direction: 'right',
					offset: [10, 0],
				})
				let length = points.length
				map.on("mousemove", (param) => {
					let _points = [[points[length - 1][0], points[length - 1][1]], [param.latlng.lat, param.latlng.lng]]
					lines.setLatLngs(_points).addTo(map)
				})
				map.off("click")
				map.on("click", (param) => {
					points.push([param.latlng.lat, param.latlng.lng])
					length = points.length
					let marker = addPoint(param.latlng)
					tempLines.setLatLngs(points).addTo(map)
					let point1 = L.latLng(points[length - 1][0], points[length - 1][1])
					let point2 = L.latLng(points[length - 2][0], points[length - 2][1])
					let distance = point1.distanceTo(point2)
					marker.bindTooltip(`${distance.toFixed(2)}米`, {
						permanent: true, 
						direction: 'right',
						offset: [10, 0],
					})
				})
			}
			
			function addPoint(latlng){
				let marker = L.circleMarker(latlng, {
					radius: 6,
					color: '#E8542B',
					fillColor: '#fff',
					fillOpacity: 1
				}).addTo(map)
				return marker
			}
		</script>
	</body>
</html>
